package com.hehui.partimejob.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hehui.partimejob.PartTimeDetailActivity;
import com.hehui.partimejob.R;
import com.hehui.partimejob.SearchActivity;
import com.hehui.partimejob.activity.ques.AddQuesActivity;
import com.hehui.partimejob.activity.signin.SignInActivity;
import com.hehui.partimejob.adapter.MyRecyclerViewAdapter;
import com.hehui.partimejob.service.inter.OnStateChangeListener;
import com.hehui.partimejob.service.model.Bean.ComTaskItemBean;
import com.hehui.partimejob.items.adapterItem.MainFunctionNavItem;
import com.hehui.partimejob.service.model.Bean.RecTaskItemBean;
import com.hehui.partimejob.service.model.Bean.UserCompleteTaskItemBean;
import com.hehui.partimejob.service.model.ComTaskItemModel;
import com.hehui.partimejob.service.model.LoopModel;
import com.hehui.partimejob.service.model.PartTimeModel;
import com.hehui.partimejob.service.model.RecTaskItemModel;
import com.hehui.partimejob.service.model.UserCompleteTaskItemModel;
import com.hehui.partimejob.view.MarqueeTextView;
import com.nex3z.flowlayout.FlowLayout;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainFragment extends BaseFragment implements OnStateChangeListener {

    private static final int LOOP_TIME = 5000;
    private static final int USER_COMPLETE_LOOP_TIME = 2000;
    private Banner loopBanner;
    private RecyclerView recRv;
    private MyRecyclerViewAdapter<PartTimeModel> partTimeAdapter;
    private List<PartTimeModel> partTimeModelList;
    private Banner userComplete;
    private RecyclerView funNavRv;
    private RelativeLayout recTaskContainer;
    private ImageView recIcon;
    private ImageView refresh;
    private RecyclerView recTaskNv;
    private RelativeLayout comTaskContainer;
    private ImageView comIcon;
    private RecyclerView comTaskNv;
    private MarqueeTextView marqueeText;
    private SmartRefreshLayout refreshLayout;
    private ClassicsHeader header;


    @Override
    public void init() {
        getNav().setTitle(R.string.index).isShowBack(false);
//        CircleImageView search = new CircleImageView(A);
//        search.setImageResource(R.drawable.search);
//        getNav().getNavRightView().addView(search);
//        search.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                A.startActivity(SearchActivity.class);
//            }
//        });

        marqueeText = findViewById(R.id.marqueeText);
        loopBanner = findViewById(R.id.loopBanner);
        recRv = (RecyclerView) findViewById(R.id.recRv);
        userComplete = findViewById(R.id.userComplete);
        funNavRv = (RecyclerView) findViewById(R.id.funNavRv);
        recTaskContainer = (RelativeLayout) findViewById(R.id.recTaskContainer);
        recIcon = (ImageView) findViewById(R.id.rec_icon);
        refresh = (ImageView) findViewById(R.id.refresh);
        recTaskNv = (RecyclerView) findViewById(R.id.recTaskNv);
        comTaskContainer = (RelativeLayout) findViewById(R.id.comTaskContainer);
        comIcon = (ImageView) findViewById(R.id.com_icon);
        comTaskNv = (RecyclerView) findViewById(R.id.comTaskNv);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        header = (ClassicsHeader) findViewById(R.id.header);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void begin(Bundle bundle) {


        marqueeText.requestFocus();
        marqueeText.setText(getString(R.string.marqueeHint));


// 大轮播图设置
        LoopModel loopModel = new LoopModel(A);
        loopModel.setListener(this);
        loopModel.loadLoopBanner();


        // 用户完成轮播设置
        UserCompleteTaskItemModel userCompleteTaskItemModel = new UserCompleteTaskItemModel(A);
        userCompleteTaskItemModel.setListener(this);
        userCompleteTaskItemModel.loadUserCompleteTask();


//        金刚区

        funNavRv.setAdapter(new MyRecyclerViewAdapter<MainFunctionNavItem>(getMainFunNavItems(), R.layout.rv_main_fun_nav_item) {
            @Override
            protected void handleData(InnerHolder holder, MainFunctionNavItem item) {
                holder.setText(R.id.title, item.getTitle()).setImgDrawable(R.id.icon, item.getIcon())
                        .setOnClickListener(R.id.item, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                TextView title = holder.getView(R.id.title);
                                switch (title.getText().toString()) {
                                    case "签到有礼":
                                       A.startActivity(SignInActivity.class);
                                        break;
                                    case "添加问卷":
                                        A.startActivity(AddQuesActivity.class);
                                        break;
                                    default:
                                }
                            }
                        });
            }
        });


//        推荐任务设置
        RecTaskItemModel recTaskItemModel = new RecTaskItemModel(A);
        recTaskItemModel.setListener(this);
        recTaskItemModel.loadRecTask();


//        社区任务设置
        ComTaskItemModel comTaskItemModel = new ComTaskItemModel(A);
        comTaskItemModel.setListener(this);
        comTaskItemModel.loadComTask();


//        partTimeModelList = new ArrayList<>();
////        ?page=1&size=1
//        MyNetwork.getInstance(A).get("/partTime/", new OnNetworkListener<PartTimeModel>() {
//            @Override
//            public void handleListData(ServiceListDataVo<PartTimeModel> data) {
//                super.handleListData(data);
//                partTimeModelList.addAll(data.getListData());
//                recRv.setAdapter(partTimeAdapter = new MyRecyclerViewAdapter<PartTimeModel>(partTimeModelList, R.layout.rv_part_time_item) {
//
//                    @Override
//                    protected void handleData(InnerHolder holder, PartTimeModel item) {
//                        Glide.with(A).load(item.getImg()).into((ImageView) holder.getView(R.id.pic));
//                        holder.setText(R.id.yuan, item.getPrice() + getString(R.string.yuan))
//                                .setText(R.id.title, getString(R.string.partTimeTitle) + item.getTitle())
//                                .setText(R.id.address, getString(R.string.partTimeAddress) + item.getAddress());
//                        String labelStr = item.getLabel();
//                        String[] label = labelStr.split("&");
//                        FlowLayout flowLayout = holder.getView(R.id.labelFl);
//                        for (String s : label) {
//                            TextView textView = new TextView(A);
//                            textView.setHeight(DisplayUtil.dp2px(A, 30));
//                            textView.setBackgroundResource(R.drawable.fl_layout_item_bg);
//                            textView.setGravity(Gravity.CENTER);
//                            textView.setTextColor(A.getColor(R.color.pri_red));
//                            textView.setPadding(DisplayUtil.dp2px(A, 16), DisplayUtil.dp2px(A, 3), DisplayUtil.dp2px(A, 16), DisplayUtil.dp2px(A, 3));
//                            textView.setText(s);
//                            flowLayout.setChildSpacing(DisplayUtil.dp2px(A, 8));
//                            flowLayout.addView(textView);
//                        }
//
////                        事件
//                        holder.getView(R.id.item).setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                A.startActivity(PartTimeDetailActivity.class);
//                            }
//                        });
//                    }
//                });
//            }
//        });
    }

    private List<MainFunctionNavItem> getMainFunNavItems() {
        ArrayList<MainFunctionNavItem> mainFunctionNavItems = new ArrayList<>();
        String[] stringArray = getResources().getStringArray(R.array.main_fun_nav_title);
        @SuppressLint("Recycle") TypedArray typedArray = getResources().obtainTypedArray(R.array.main_fun_nav_icon);
        for (int i = 0; i < stringArray.length; i++) {
            Drawable icon = typedArray.getDrawable(i);
            MainFunctionNavItem mainFunctionNavItem = new MainFunctionNavItem();
            mainFunctionNavItem.setIcon(icon);
            mainFunctionNavItem.setTitle(stringArray[i]);
            mainFunctionNavItems.add(mainFunctionNavItem);
        }
        return mainFunctionNavItems;
    }


    @Override
    public void OnStateChanged(String tag, List data) {
        switch (tag) {
            case "LoopModel":
                loopBanner.setAdapter(new BannerImageAdapter<Integer>(data) {
                    @Override
                    public void onBindView(BannerImageHolder holder, Integer data, int position, int size) {
                        holder.imageView.setImageResource(data);
                    }

                }).setIndicator(new CircleIndicator(loopBanner.getContext()))
                        .setLoopTime(LOOP_TIME)
                        .addBannerLifecycleObserver(A);
                break;
            case "UserCompleteTaskItemModel":
                userComplete.setAdapter(new BannerAdapter<UserCompleteTaskItemBean, MyRecyclerViewAdapter.InnerHolder>(data) {

                    @Override
                    public MyRecyclerViewAdapter.InnerHolder onCreateHolder(ViewGroup parent, int viewType) {
                        return new MyRecyclerViewAdapter.InnerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.loop_usercomplete_item, parent, false));
                    }

                    @Override
                    public void onBindView(MyRecyclerViewAdapter.InnerHolder holder, UserCompleteTaskItemBean data, int position, int size) {
                        holder.setText(R.id.username, data.getUsername())
                                .setText(R.id.hint, getString(R.string.usercomplete_hint))
                                .setText(R.id.coin, data.getCoin() + getString(R.string.coin));
                        CircleImageView circleImageView = holder.getView(R.id.pic);
                        circleImageView.setImageDrawable(data.getPic());
                    }

                }).setLoopTime(USER_COMPLETE_LOOP_TIME)
                        .addBannerLifecycleObserver(A)
                        .setUserInputEnabled(false);
                break;
            case "RecTaskItemModel":
                recTaskNv.setAdapter(new MyRecyclerViewAdapter<RecTaskItemBean>(data, R.layout.rv_rectask_item) {
                    @Override
                    protected void handleData(InnerHolder holder, RecTaskItemBean item) {
                        holder.setText(R.id.title, item.getTitle())
                                .setText(R.id.coin, item.getCoin() + getString(R.string.coin))
                                .setText(R.id.surplus, getString(R.string.surplus) + item.getSurplus() + getString(R.string.fen))
                                .setText(R.id.hint, getString(R.string.mostValue))
                                .setImgDrawable(R.id.icon, A.getDrawable(R.drawable.questionnaire));
                    }

                });
                break;
            case "ComTaskItemModel":
                comTaskNv.setAdapter(new MyRecyclerViewAdapter<ComTaskItemBean>(data, R.layout.rv_comtask_item) {
                    @Override
                    protected void handleData(InnerHolder holder, ComTaskItemBean item) {
                        int type = item.getType();
                        holder.setText(R.id.title, item.getTitle())
                                .setText(R.id.hint, getString(R.string.pinTask))
                                .setText(R.id.dateTime, item.getDateTime())
                                .setBgDrawable(R.id.pic, item.getPic())
                                .setText(R.id.comment, item.getCommentNum() + getString(R.string.comment))
                                .setVisibility(R.id.top, item.isTop())
                                .setVisibility(R.id.discuss, type == 1)
                                .setVisibility(R.id.topic, type == 2);

                    }

                });
                break;
            default:
                break;
        }
    }
}
